﻿using MediatR;
using T.Settlement.Domain;
using T.Settlement.Domain.DTO;
using T.Settlement.Read.API.Application.Command.ExpensesCommand;
using T.Settlement.Repository.SettlementInterface;

namespace T.Settlement.Read.API.Application.CommandHandlers.ExpensesHandler
{
    public class ExpensesShowCommandHandler : IRequestHandler<ExpensesShowCommand, SListDTO<Expenses>>
    {
        /// <summary>
        /// 应付/收费仓储接口
        /// </summary>
        private readonly IExpensesSRepository _expensesSRepository;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="expensesSRepository">应付/收费仓储接口</param>
        public ExpensesShowCommandHandler(IExpensesSRepository expensesSRepository)
        {
            _expensesSRepository = expensesSRepository;
        }
        /// <summary>
        /// 显示应付/收费
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<SListDTO<Expenses>> Handle(ExpensesShowCommand request, CancellationToken cancellationToken)
        {
            var list = (await _expensesSRepository.GetAllAsync()).Where(x=>!x.IsDeleted);
            if (!string.IsNullOrEmpty(request.CostManagementAttn))
            {
                list = list.Where(x => x.CostManagementAttn.Contains(request.CostManagementAttn)).ToList();
            }
            if (!string.IsNullOrEmpty(request.OutsourcedUnits))
            {
                list = list.Where(x => x.OutsourcedUnits.Contains(request.OutsourcedUnits)).ToList();
            }
            if (!string.IsNullOrEmpty(request.ShipperIsUnit))
            {
                list = list.Where(x => x.ShipperIsUnit.Contains(request.ShipperIsUnit)).ToList();
            }
            if (request.CheckStatus != null)
            {
                list = list.Where(x => x.CheckStatus == request.CheckStatus).ToList();
            }
            if (!string.IsNullOrEmpty(request.PaymentMethod))
            {
                list = list.Where(x => x.PaymentMethod == request.PaymentMethod).ToList();
            }
            if (request.BusinessDate != null)
            {
                list = list.Where(x => x.BusinessDate == Convert.ToDateTime(request.BusinessDate).AddDays(1)).ToList();
            }
            if (request.ExpensesClassify != 0)
            {
                list = list.Where(x => x.ExpensesClassify == request.ExpensesClassify).ToList();
            }
            if (!string.IsNullOrEmpty(request.CompanyID))
            {
                list=list.Where(x=>x.CompanyID== request.CompanyID).ToList();
            }
            if (request.PageIndex == null)
            {
                request.PageIndex = 1;
            }
            if (request.PageSize==null)
            {
                request.PageSize = list.Count();
            }
            SListDTO<Expenses> page = new SListDTO<Expenses>();
            page.TotalCount = list.Count();
            page.TotalPage = (int)Math.Ceiling((double)(page.TotalCount * 1.0 / request.PageSize));
            page.List = list.OrderByDescending(x => x.CreatedDate).Skip((int)((request.PageIndex - 1) * request.PageSize)).Take((int)request.PageSize).ToList();
            return page;
        }
    }
}
